﻿using Entidades;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccesoDatos
{
    public class ADContrato
    {
        Database oDatabase = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["conexionBD"]);

        public List<ENContrato> ContratoListarPag(
       int pagina, int cantRegxPag)
        {
            DbCommand odbcommand = oDatabase.GetStoredProcCommand("USP_SEL_CONTRATO_PAG", pagina, cantRegxPag);

            List<ENContrato> lista = new List<ENContrato>();
            using (IDataReader reader = oDatabase.ExecuteReader(odbcommand))
            {
                while (reader.Read())
                    lista.Add(new ENContrato(reader, 1));
            }
            return lista;
        }

        public ENContrato ContratoDetalle(int idContrato)
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("USP_SEL_CONTRATO", idContrato);
            ENContrato contrato = new ENContrato();

            using (IDataReader reader = oDatabase.ExecuteReader(oDbCommand))
            {
                if (reader.Read())
                    contrato = new ENContrato(reader);
            }
            return contrato;
        }

        public void GuardarContrato(ENContrato collecion)
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("USP_CONTRATO_I");
            oDatabase.AddInParameter(oDbCommand, "@piiClienteID", DbType.Int32, collecion.Cliente_ID);
            oDatabase.AddInParameter(oDbCommand, "@pidtFechaSalida", DbType.Date, collecion.FechaSalida);
            oDatabase.AddInParameter(oDbCommand, "@pidtFechaLlegada", DbType.Date, collecion.FechaLlegada);
            oDatabase.AddInParameter(oDbCommand, "@pisRuta", DbType.String, collecion.Ruta);
            oDatabase.AddInParameter(oDbCommand, "@pisAerolinea", DbType.String, collecion.Aerolinea);
            oDatabase.AddInParameter(oDbCommand, "@pisVuelo", DbType.String, collecion.Vuelo);
            oDatabase.AddInParameter(oDbCommand, "@piiNumeroMaletas", DbType.Int32, collecion.NumeroMaletas);
            oDatabase.AddInParameter(oDbCommand, "@pidcPrecio", DbType.Decimal, collecion.Precio);
            oDatabase.AddInParameter(oDbCommand, "@pidcTotal", DbType.Decimal, collecion.Total_cnt);
            oDatabase.AddInParameter(oDbCommand, "@pidcPeso", DbType.Decimal, collecion.Peso);
            oDatabase.AddInParameter(oDbCommand, "@pisTipoEntrega", DbType.String, collecion.TipoEntrega);
            oDatabase.AddInParameter(oDbCommand, "@pisGastos", DbType.String, collecion.Gastos);
            oDatabase.AddInParameter(oDbCommand, "@pisObservacion", DbType.String, collecion.Observacion);
            oDatabase.AddInParameter(oDbCommand, "@pisDescripcion", DbType.String, collecion.Descripcion);
            oDatabase.AddInParameter(oDbCommand, "@pisDatosMaleta", DbType.String, collecion.DatosMaleta);
            oDatabase.AddInParameter(oDbCommand, "@pisEstado", DbType.String, collecion.Cod_Estado);
            //oDatabase.AddInParameter(oDbCommand, "correo", DbType.Date, collecion.FechaLlegada);
            oDatabase.ExecuteNonQuery(oDbCommand);
            //DataSet db = oDatabase.ExecuteDataSet(oDbCommand);
            //string info = db.Tables[0].Rows[0][0].ToString();
            //if(info != null)
            //return info;

        }

        public void ActualizarContrato(ENContrato collecion)
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("usp_Contrato_u");
            //@piiID
            oDatabase.AddInParameter(oDbCommand, "@piiID", DbType.Int32, collecion.ID);
            oDatabase.AddInParameter(oDbCommand, "@piiClienteID", DbType.String, collecion.Cliente_ID);
            oDatabase.AddInParameter(oDbCommand, "@pidtFechaSalida", DbType.Date, collecion.FechaSalida);
            oDatabase.AddInParameter(oDbCommand, "@pidtFechaLlegada", DbType.Date, collecion.FechaLlegada);
            oDatabase.AddInParameter(oDbCommand, "@pisRuta", DbType.String, collecion.Ruta);
            oDatabase.AddInParameter(oDbCommand, "@pisAerolinea", DbType.String, collecion.Aerolinea);
            oDatabase.AddInParameter(oDbCommand, "@pisVuelo", DbType.String, collecion.Vuelo);
            oDatabase.AddInParameter(oDbCommand, "@piiNumeroMaletas", DbType.Int32, collecion.NumeroMaletas);
            oDatabase.AddInParameter(oDbCommand, "@pidcPrecio", DbType.Decimal, collecion.Precio);
            oDatabase.AddInParameter(oDbCommand, "@pidcTotal", DbType.Decimal, collecion.Total_cnt);
            oDatabase.AddInParameter(oDbCommand, "@pidcPeso", DbType.Decimal, collecion.Peso);
            oDatabase.AddInParameter(oDbCommand, "@pisTipoEntrega", DbType.String, collecion.TipoEntrega);
            oDatabase.AddInParameter(oDbCommand, "@pisGastos", DbType.String, collecion.Gastos);
            oDatabase.AddInParameter(oDbCommand, "@pisObservacion", DbType.String, collecion.Observacion);
            oDatabase.AddInParameter(oDbCommand, "@pisDescripcion", DbType.String, collecion.Descripcion);
            oDatabase.AddInParameter(oDbCommand, "@pisDatosMaleta", DbType.String, collecion.DatosMaleta);
            oDatabase.AddInParameter(oDbCommand, "@pisEstado", DbType.String, collecion.Cod_Estado);            
            oDatabase.ExecuteNonQuery(oDbCommand);
        }

        public void EliminarContrato(int idContrato)
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("usp_Contrato_u_Estado", idContrato);
            oDatabase.ExecuteNonQuery(oDbCommand);
        }
    }
}
